//
//  SpeedUpView.m
//  MessangerSkeleton
//
//  Created by User-16 on 4/28/14.
//  Copyright (c) 2014 IOTrainingCamp. All rights reserved.
//

#import "SpeedUpView.h"
#import "MessengerSprite.h"
#import "Configuration.h"

@interface SpeedUpView ()

@property id powerUpObserver;
@property (nonatomic, readwrite) NSUInteger powerUpSpeedBoostCollectedItemsCount;
@end

@implementation SpeedUpView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setBackgroundColor: [UIColor colorWithRed:100/255.0 green:126/255.0 blue:100/255.0 alpha:0.5]];
        
        //setup progress bar
        self.progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
        self.progressBar.frame = CGRectMake(2, self.frame.size.height/2,  55, self.frame.size.height);
        self.progressBar.progress = 0;
        self.progressBar.trackTintColor = [UIColor colorWithRed:112/255.0f green:76/255.0f blue:41/255.0f alpha:0.5];
        self.progressBar.progressTintColor = [UIColor colorWithRed:112/255.0f green:76/255.0f blue:41/255.0f alpha:1.0];
        [self addSubview: self.progressBar];
        
        //setup turbo button
        self.turboButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        self.turboButton.frame = CGRectMake( 60, 0, self.frame.size.width - 55, self.frame.size.height);
        [self.turboButton setTitleColor:[UIColor colorWithRed:112/255.0f green:76/255.0f blue:41/255.0f alpha:0.5] forState:UIControlStateDisabled];
        [self.turboButton setTitleColor:[UIColor colorWithRed:112/255.0f green:76/255.0f blue:41/255.0f alpha:1.0] forState:UIControlStateNormal];
        [self.turboButton addTarget:self action:@selector(boost:) forControlEvents:UIControlEventTouchUpInside];
        [self.turboButton setTitle: @"Turbo" forState: UIControlStateNormal];
        self.turboButton.titleLabel.font = [UIFont fontWithName:kSceneButtonFontNameVerdanaBold size:18];
        self.turboButton.enabled = NO;
        [self addSubview:self.turboButton];
        
        self.clipsToBounds = NO;
        
        self.powerUpObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kNotificationPlayerPowerUpChange object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {

                if(self.player.powerUpSpeedBoostCollectedItemsCount  < kMessengerPowerUpsNeededForTurbo)
                    self.progressBar.progress = (double)self.player.powerUpSpeedBoostCollectedItemsCount / kMessengerPowerUpsNeededForTurbo;
                else {
                    self.progressBar.progress = 1;
                    self.turboButton.enabled = YES;
                }
            
            }];
        }
                                return self;
                                }


- (IBAction)boost:(id)sender {
                                    
                                    self.turboButton.enabled = NO;
                                    self.progressBar.progress = 0;
    [self.player   turboBoost];
    
                                    //boost the player
                                }
                                
                                -(void) dealloc
        {
            [[NSNotificationCenter defaultCenter] removeObserver:self.powerUpObserver];
        }
                                
                                
                                
                                
                                
        /*
         // Only override drawRect: if you perform custom drawing.
         // An empty implementation adversely affects performance during animation.
         - (void)drawRect:(CGRect)rect
         {
         // Drawing code
         }
         */
                                
                                @end
